import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:share_fit/app/components/back_btn_icon.dart';
import 'package:share_fit/app/helper/ext/tr_ext.dart';
import 'package:share_fit/app/tools/color_const.dart';
import 'package:share_fit/generated/locales.g.dart';
import 'package:sprintf/sprintf.dart';

import 'quick_reply_logic.dart';

class QuickReplyPage extends StatelessWidget {
  QuickReplyPage({super.key});

  final logic = Get.put(QuickReplyLogic());
  final state = Get
      .find<QuickReplyLogic>()
      .state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          title: Text(LocaleKeys.settings_quick_reply.ts, style: TextStyle(fontSize: 54.sp)),
          centerTitle: true,
          leading: BackBtnIcon(onTap: () => Get.back())
      ),

      body: Padding(
          padding: EdgeInsets.only(left: 40.w, top: 80.h, right: 40.w),
          child: GetBuilder<QuickReplyLogic>(builder: (logic) {
            return ListView(
              children: _buildWidgets(context),
            );
          })),
    );
  }

  List<Widget> _buildWidgets(BuildContext context) {
    List<Widget> widgets = [];

    widgets.add(_buildSwitchWidget(context));
    widgets.add(SizedBox(height: 55.h));

    if (state.smsReplyEnable) {

      if (state.fixedReplyList.isNotEmpty) {
        widgets.add(_buildReplyList(context, state.fixedReplyList, true));
        widgets.add(SizedBox(height: 55.h));
      }

      if (state.replyList.isNotEmpty) {
        widgets.add(_buildReplyList(context, state.replyList, false));
        widgets.add(SizedBox(height: 55.h));
      }
      widgets.add(_buildAddWidget(context));
    }

    return widgets;
  }

  Widget _buildSwitchWidget(BuildContext context) {
    return Container(
      height: 175.h,
      padding: EdgeInsets.only(left: 59.w, right: 36.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(20),
      ),
      child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              LocaleKeys.settings_quick_reply.ts,
              style: TextStyle(
                  fontSize: 42.sp,
                  color: ColorConst.primaryFontColor),
            ),
            Transform.scale(
                scale: 0.7,
                child: CupertinoSwitch(
                    value: state.smsReplyEnable,
                    activeColor: ColorConst.accentColor,
                    onChanged: (value) {
                      logic.setSmsReplyEnable(value);
                    }))
          ]),
    );
  }

  Widget _buildReplyList(BuildContext context, List replyList, bool isFixed) {
    return Container(
      padding: EdgeInsets.only(left: 59.w, right: 36.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(20),
      ),
      child: Column(
        children: replyList
            .map((e) => _buildReplyWidgets(context, e, isFixed))
            .toList(),
      ),
    );
  }

  Widget _buildReplyWidgets(BuildContext context, content, isFixed) {
    return Ink(
      height: 155.h,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(20),
      ),
      child: InkResponse(
        splashColor: ColorConst.activeColor,
        highlightColor: ColorConst.activeColor,
        borderRadius: BorderRadius.circular(20),
        highlightShape: BoxShape.rectangle,
        containedInkWell: true,
        child: ListTile(
          contentPadding: EdgeInsets.only(left: 1.w),
          leading: isFixed ? null: IconButton(
              onPressed: () => {
                _showAddDialog(context, content, false)
              },
              icon: const Icon(Icons.edit)),
          title: Text(content, style: TextStyle(fontSize: 42.sp)),
          trailing: isFixed ? null: IconButton(
              onPressed: () => {
                // if (state.replyList.length == 1) {
                //   logic.showTips(LocaleKeys.info.ts, LocaleKeys.quick_reply_min.ts)
                // }
                // else {
                //   _showDeleteDialog(context, content)
                // }
                _showDeleteDialog(context, content)
              },
              icon: const Icon(Icons.delete)),
        ),
      ),
    );
  }

  Widget _buildAddWidget(BuildContext context) {
    return Ink(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(20),
      ),
      child: InkResponse(
        splashColor: ColorConst.activeColor,
        borderRadius: BorderRadius.circular(20),
        highlightShape: BoxShape.rectangle,
        highlightColor: ColorConst.activeColor,
        containedInkWell: true,
        child: SizedBox(
            height: 175.h,
            child: Center(
              child: Image.asset(
                "assets/images/device/icon_remind_add.png", width: 50.w,
                height: 50.w,),
            )
        ),
        onTap: () {
          if (state.replyList.length == state.maxReplyCount) {
            logic.showTips(LocaleKeys.info.ts, sprintf(LocaleKeys.quick_reply_max.ts, [state.maxReplyCount]));
          }
          else {
            _showAddDialog(context, "", true);
          }
        },
      ),
    );
  }

  void _showAddDialog(BuildContext context, String content, bool isAdd) {
    Get.closeAllSnackbars();
    final textCtrl = TextEditingController();
    textCtrl.text = content;
    final theme = Theme.of(context);
    Get.defaultDialog(
      title: isAdd ? LocaleKeys.quick_reply_add.ts : LocaleKeys.quick_reply_edit
          .ts,
      textConfirm: LocaleKeys.ok.ts,
      textCancel: LocaleKeys.cancel.ts,
      buttonColor: ColorConst.accentColor,
      onConfirm: () =>
      {
        Get.back(),
        if (isAdd) {
          logic.addReply(textCtrl.text)
        }
        else {
          logic.replaceReply(content, textCtrl.text)
        }
      },
      content: Container(
        padding: EdgeInsets.only(top: 5.h, bottom: 5.h),
        child: TextFormField(
          autofocus: true,
          inputFormatters: [LengthLimitingTextInputFormatter(256)],
          maxLines: 3,
          decoration: InputDecoration(
            contentPadding: const EdgeInsets.symmetric(
                vertical: 10.0, horizontal: 10.0),
            border: OutlineInputBorder(
              borderSide:
              BorderSide(color: ColorConst.accentColor, width: 1),
              borderRadius: BorderRadius.circular(5),
            ),
            focusedBorder: OutlineInputBorder(
              borderSide:
              BorderSide(color: ColorConst.accentColor, width: 2),
              borderRadius: BorderRadius.circular(5),
            ),
          ),
          controller: textCtrl,
        ),
      ),
    );
  }

  void _showDeleteDialog(BuildContext context, content) {
    final theme = Theme.of(context);
    Get.closeAllSnackbars();
    Get.defaultDialog(
        title: LocaleKeys.info.ts,
        middleText: LocaleKeys.quick_reply_delete_tips.ts,
        textConfirm: LocaleKeys.ok.ts,
        textCancel: LocaleKeys.cancel.ts,
        buttonColor: ColorConst.accentColor,
        cancelTextColor: theme.buttonTheme.colorScheme?.secondary,
        onConfirm: () => {Get.back(), logic.deleteReply(content)});
  }
}
